//
//  XMEmoticoKeyboardView.swift
//  XMWeimo
//
//  Created by ios001 on 16/7/29.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class XMEmoticoKeyboardView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 216))
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        // 添加控件
        addSubview(emoyionToolbar)
        addSubview(emoticonPageView)
        addSubview(pageControl)
        // 设置约束
        emoyionToolbar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        emoticonPageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emoyionToolbar.snp_top)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonPageView).offset(5)
        }
        
        // 监听底部视图按钮点击回调
        emoyionToolbar.closure = {[weak self] (type: XMEmoticonToolbarType)->() in
            switch type{
            case .Default:
                print("默认")
            case .Emoji:
                print("Emoji")
            case .Lxh:
                print("浪小花")
            }
            // 获取index
            let indexPath: NSIndexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            // 滚动emoticonPageView
            self?.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            // 设置pageControl
            self?.setupPageControlData(indexPath)
        }
        // 设置pageControl
        let indexPath = NSIndexPath(forItem: 0, inSection: 0)
        setupPageControlData(indexPath)
    }
    
    // MARK: - 懒加载控件
    // 底部视图
    private lazy var emoyionToolbar: XMEmoticonToolbar = XMEmoticonToolbar(frame: CGRectZero)
    // 表情view
    private lazy var emoticonPageView: XMEmoticonPageView = {
        let view = XMEmoticonPageView()
        view.backgroundColor = self.backgroundColor
        // 分页
        view.pagingEnabled = true
        // 设置代理
        view.delegate = self
        // 弹簧效果
        view.bounces = false
        // 隐藏滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        return view
        }()
    // 分页
    private lazy var pageControl: UIPageControl = {
        let view = UIPageControl()
        // 设置总页数
        // 在xcode 6.4可以看到,后来版本隐藏了,知道名字可以通过kvc赋值
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        return view
    }()
}

extension XMEmoticoKeyboardView: UICollectionViewDelegate {
    // 实时监听UICollectionView滚动
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 获取相对于collectionView 中心点X
        let centerX = scrollView.contentOffset.x + SCREENW*0.5
//        print(centerX)
        // 拿到屏幕显示的cell 数组 对数组中的元素进行排序
        let cells = self.emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        // 如果屏幕显示2个 才做判断
        if cells.count == 2 {
            let firstCell = cells.first!
            let lastCell = cells.last!
            var indexPath: NSIndexPath
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
                indexPath = self.emoticonPageView.indexPathForCell(firstCell)!
            }else {
                indexPath = self.emoticonPageView.indexPathForCell(lastCell)!
            }
            // 改变toolbar中按钮的状态
            emoyionToolbar.setupCurrentButton(indexPath.section)
            
            // 设置pageControl
            setupPageControlData(indexPath)
        }
    }
    // 设置pageControl
    func setupPageControlData(indexPath: NSIndexPath) {
        pageControl.numberOfPages = XMEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
}
